﻿using KwaiXiaoDian.Entity.VO.GroupLeader;
using KwaiXiaoDian.Entity.VO.Product;
using KwaiXiaoDian.Entity.VO.User;
using KwaiXiaoDian.IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SSharing.Frameworks.NetCore.Common.Consts;
using SSharing.Frameworks.NetCore.Common.Extends;
using SSharing.Frameworks.NetCore.Common.Helpers;
using SSharing.Frameworks.NetCore.Common.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace KwaiXiaoDian.OpenApi.Controllers
{
    /// <summary>
    /// 商品管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly IGroupLeaderService _groupLeaderService;
        private readonly IProductService _productService;
        private readonly IProductShopService _productShopService;
        private readonly IProductRankService _productRankService;
        private readonly IChannelService _channelService;
        private readonly IProductPromoteService _productPromoteService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupLeaderService"></param>
        /// <param name="productService"></param>
        /// <param name="productShopService"></param>
        /// <param name="productRankService"></param>
        /// <param name="channelService"></param>
        /// <param name="productPromoteService"></param>
        public ProductController(IGroupLeaderService groupLeaderService
            , IProductService productService
            , IProductShopService productShopService
            , IProductRankService productRankService
            , IChannelService channelService
            , IProductPromoteService productPromoteService)
        {
            _groupLeaderService = groupLeaderService;
            _productService = productService;
            _productShopService = productShopService;
            _productRankService = productRankService;
            _channelService = channelService;
            _productPromoteService = productPromoteService;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("getpagingproducts")]
        public ActionResult<ApiResult<IPagingResult<Product>>> GetPagingProducts(GetPagingProductsRequest request)
        {
            var apiResult = new ApiResult<IPagingResult<Product>>();

            //入参校验
            if (request == null)
            {
                request = new GetPagingProductsRequest()
                {
                    Page = 1,
                    Limit = 10
                };
            }
            if (request.Page <= 0)
            {
                request.Page = 1;
            }
            if (request.Limit <= 0)
            {
                request.Limit = 10;
            }

            var pagingProducts = _productService.GetPagingProducts(request);
            apiResult.Data = pagingProducts;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 获取商品店铺信息
        /// </summary>
        /// <param name="productId">商品ID</param>
        /// <param name="distributeItemId">商品分发ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("getshopinfo")]
        public ActionResult<ApiResult<ShopInfo>> GetShopInfo(string productId, string distributeItemId)
        {
            var apiResult = new ApiResult<ShopInfo>();

            var shopInfo = _productShopService.GetShopInfo(productId, distributeItemId);
            apiResult.Data = shopInfo;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 获取商品达人排行榜
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("getpagingranks")]
        public ActionResult<ApiResult<IPagingResult<Rank>>> GetPagingRanks(GetPagingRanksRequest request)
        {
            var apiResult = new ApiResult<IPagingResult<Rank>>();

            //入参校验
            if (request == null)
            {
                request = new GetPagingRanksRequest()
                {
                    Page = 1,
                    Limit = 10
                };
            }
            if (request.Page <= 0)
            {
                request.Page = 1;
            }
            if (request.Limit <= 0)
            {
                request.Limit = 50;
            }

            var pagingRanks = _productRankService.GetPagingRanks(request);
            apiResult.Data = pagingRanks;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 获取全部Channel列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("getallchannels")]
        public ActionResult<ApiResult<List<Channel>>> GetAllChannels()
        {
            var apiResult = new ApiResult<List<Channel>>();

            var channels = _channelService.GetAllChannels().ToList();
            apiResult.Data = channels;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 获取导出字段列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("getexportfields")]
        public ActionResult<ApiResult<IList<ExportField>>> GetExportFields()
        {
            var apiResult = new ApiResult<IList<ExportField>>();

            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value.ToLong();
            var role = User.FindFirst(ClaimTypes.Role).Value;
            var exportFields = _productService.GetExportFields();
            apiResult.Data = exportFields;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("export")]
        public ActionResult Export(ExportRequest request)
        {
            //入参校验
            if (request == null)
            {
                request = new ExportRequest();
            }

            var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            var fileName = string.Empty;

            //文件名
            fileName = string.Format("{0}.xlsx", request.TaskName);

            byte[] exportResponse = _productService.Export(request);

            return File(exportResponse, contentType, fileName);
        }

        /// <summary>
        /// 获取商品推广数据
        /// </summary>
        /// <param name="productId">商品Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("getpromotedata")]
        public ActionResult<ApiResult<GetPromoteDataResponse>> GetPromoteData(string productId)
        {
            var apiResult = new ApiResult<GetPromoteDataResponse>();

            var getPromoteDataResponse = _productPromoteService.GetPromoteData(productId);
            apiResult.Data = getPromoteDataResponse;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 获取商品SKU列表
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("getskulist")]
        public ActionResult<ApiResult<GetSKUListResponse>> GetSKUList(string productId)
        {
            var apiResult = new ApiResult<GetSKUListResponse>();

            //入参校验
            if (productId.IsNullOrEmpty())
            {
                apiResult.ErrorMessage = "商品Id不能为空!";
                return apiResult;
            }

            var getSKUListResponse = _productService.GetSKUList(productId);
            apiResult.Data = getSKUListResponse;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 更新处理意见
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("updatehandleremark")]
        public ActionResult<ApiResult> UpdateHandleRemark(UpdateHandleRemarkRequest request)
        {
            var apiResult = new ApiResult();

            //入参校验
            if (request == null)
            {
                request = new UpdateHandleRemarkRequest();
            }
            if (request.HandleStatus.IsNullOrEmpty())
            {
                apiResult.ErrorMessage = "处理状态不能为空!";
                return apiResult;
            }

            //获取登录用户
            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value.ToLong(0);
            var role = User.FindFirst(ClaimTypes.Role).Value;
            var updateHandleRemarkResult = _productService.UpdateHandleRemark(request, userId);
            if (updateHandleRemarkResult.Item1 != true)
            {
                apiResult.ErrorMessage = updateHandleRemarkResult.Item2;
                return apiResult;
            }
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

    }
}
